package Cam3zyx0712;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Scanner;

public class DrawMouse implements ActionListener {
    private Graphics g;
    private Graphics bg;
    private BufferedImage bufferedImage;
    String fn ="C:\\Users\\周园鑫\\Desktop\\flower.jpg";

    public DrawMouse(Graphics g) {
        this.g = g;
    }

    public void actionPerformed(ActionEvent e) {
        String btnStr = e.getActionCommand();
        System.out.println("用户点击了" + btnStr+"按钮......");
        System.out.println("稍等一会，图片效果马上呈现");
        //根据字符串绘制相关图片
        switch (btnStr) {
            case "原图":
                oripic();
                break;
            case "灰度"://取rgb平均值，则画成灰度图
                grey();
                break;
            case "二值化":
                binarization();//
                break;
            case "马赛克":
                mosaic();
                break;
            case"放大":
                Todouble();
        }
    }
    public void bufferZone(){
        int[][] data = image(fn);
        //程序的执行过程：代码 > jvm > os > 总线 > 显示器
        //创建缓冲区 把所有的像素点显示在缓存去上
        bufferedImage = new BufferedImage( data[0].length, data.length,BufferedImage.TYPE_INT_RGB);
        //获取缓冲区画笔
        bg = bufferedImage.getGraphics();
    }
        public void oripic(){
            bufferZone();
            int[][] data = image(fn);//图片放在项目目录下
            for (int i = 0; i < data.length; i++) {
                for (int j = 0; j < data[i].length; j++) {
                    //得到颜色值，画出每一个点
                    Color c = new Color(data[i][j]);
                    bg.setColor(c);//g.setColor(new Color(data[i][j])）；
                    bg.fillOval(i / 2, j /2, 2, 2);
                }
            }
            //把缓冲区显示在窗体；
            g.drawImage(bufferedImage,0,0,null);
        }
    public void grey(){
        bufferZone();
        int[][] data = image(fn);
        for (int i = 0; i < data.length; i++){
            for (int j = 0; j < data[i].length; j++){
                Color color = new Color(data[i][j]);
                int red = color.getRed();
                int green = color.getGreen();
                int blue = color.getBlue();
                int sum = (red+green+blue)/3;
                Color newColor = new Color(sum,sum,sum);
                bg.setColor(newColor);
                bg.drawOval(i/2,j/2,1,1);
            }
        }
        //把缓冲区显示在窗体；
        g.drawImage(bufferedImage,0,0,null);
    }

    public void mosaic() {
        bufferZone();
        int[][] data = image(fn);
        for (int i = 0; i < data.length; i += 15) {
            for (int j = 0; j < data[i].length; j += 15) {
                int piexl2 = data[i][j];
                Color color = new Color(piexl2);
                bg.setColor(color);
                bg.fillRect(i / 2, j / 2, 15, 15);
            }
        }
        //把缓冲区显示在窗体；
        g.drawImage(bufferedImage,0,0,null);
    }

    public void binarization() {
        bufferZone();
        int[][] data = image(fn);
        int h = data.length;
        int w = data[0].length;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                int p = data[i][j];
                Color color = new Color(p);
                int blue = color.getBlue();
                if (blue < 100){
                    bg.setColor(Color.BLACK);
                }else{
                    bg.setColor(Color.WHITE);
                }
                bg.drawOval(i/2,j/2,1,1);
            }
        }
        //把缓冲区显示在窗体；
        g.drawImage(bufferedImage,0,0,null);
    }

    public void Todouble() {
        bufferZone();
        int[][] data = image(fn);//图片放在项目目录下
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                //得到颜色值，画出每一个点
                Color c = new Color(data[i][j]);
                bg.setColor(c);//g.setColor(new Color(data[i][j])）；
                bg.fillOval(i / 2, j / 2, 2, 2); // 缩小原来的绘制区域
            }
        }

        // 放大图片
        while (true) {
            System.out.println("需要放大几倍：");
            Scanner input = new Scanner(System.in);
            int x = input.nextInt();
            BufferedImage outputImage = new BufferedImage(bufferedImage.getWidth() * x/2, bufferedImage.getHeight() * x/3, bufferedImage.getType());
            Graphics2D g2d = outputImage.createGraphics();
            g2d.drawImage(bufferedImage, 0, 0, outputImage.getWidth(), outputImage.getHeight(), null);
            g2d.dispose();

            // 把缓冲区显示在窗体；
            g.drawImage(outputImage, 0, 0, null);
            System.out.println("输入100结束进程");
            if(x==100){
                break;
            }
        }
    }

    //将一张图片转化一个int型的二维数组
    private int[][] image(String imageName) {
        File file = new File(imageName);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file); //从文件到图片对象
        } catch (Exception e) {
            e.printStackTrace();
        }
        int w = bi.getWidth(); //图片的宽
        int h = bi.getHeight(); //图片的高
        int[][] imIndex = new int[w][h];//存像素值的二维数组
        //System.out.println("w=" + w + " h=" + h );
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                imIndex[i][j] = bi.getRGB(i, j); //i,j 位置的 Color 值，每个像素点的 color 存入数组
            }
        }
        return imIndex;
    }
}


